#!/usr/bin/env python

import numpy as np
import pylab as pl
import os.path as osp
import sys
import matplotlib
from matplotlib.font_manager import FontProperties as FP

def poly2(a,b,c,start,end,res):

	# x and y data
	x=np.arange(start,end,res)
	y=[]

	# y=ax^2+bx+c NB: kB!	
	for val in x:
		y.append((a*x[val]**2+b*x[val]+c)/float(1000))
	
	return x,y
	

if __name__ == '__main__':

	# Fixes font size
	fp = FP()
	fp.set_size('small')

	# Use latex formatting in strings
	matplotlib.rc('text', usetex=True) 

	mtu=1400 # MTU [bytes] 
	o_h=48 # overhead from UDP header in [bytes] ipv6
	q=[1/8,1,2] # data size for a entry in the coding vector [bytes]

	# Calculate
	x1,y1=poly2(-q[0],(mtu-o_h),0,0,1000,1)
	x2,y2=poly2(-q[1],(mtu-o_h),0,0,1000,1)
	x3,y3=poly2(-q[2],(mtu-o_h),0,0,1000,1)

	# Prepare figure
	fig = pl.figure(figsize=[10,4]) # Setting aspect ratio
	fig.subplots_adjust(wspace=0.5) # spacing between subplots
	ax1 = fig.add_subplot(111) # left plot

	# Fill data
	ax1.plot(x1,y1,color='black',marker='s',mfc='none',markevery=20)
	ax1.plot(x2,y2,color='black',marker='o',mfc='none',markevery=20)
	ax1.plot(x3,y3,color='black',marker='^',mfc='none',markevery=20)

	# Plot annotation
	pl.xlabel('Generation size [-]')
	pl.ylabel('Source data in a generation [kB]')
	pl.legend(['FF(2) MTU '+str(mtu)+' bytes','FF($2^{8}$) MTU '+str(mtu)+' bytes','FF($2^{16}$) MTU '+str(mtu)+' bytes'],loc='lower right',prop=fp)
	pl.grid('on')
	pl.xticks(np.arange(0,1100,100))
	pl.yticks(np.arange(0,1100,100))
	pl.xlim(0,1000)
	pl.ylim(0,1000)

	# Save figure
	svndir = osp.abspath('../../rapport/figs')
	pl.savefig(svndir+'/'+'data_in_gen'+'.eps')




